package com.yugao.fintech.antelope.shortlink.listener;

import com.rabbitmq.client.Channel;
import com.yugao.fintech.antelope.shortlink.model.mq.SlAccessLogSaveMQ;
import com.yugao.fintech.antelope.shortlink.service.AccessLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
@RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(value = SlAccessLogSaveMQ.QUEUE, durable = "true"),
                exchange = @Exchange(value = SlAccessLogSaveMQ.EXCHANGE, type = ExchangeTypes.TOPIC))
})
public class SlAccessLogSaveListener {

    @Autowired
    private AccessLogService accessLogService;

    @RabbitHandler
    public void onMessage(@Payload String msg, Channel channel, Message message) {
        log.info("消费删除用户消息, msg: {}", msg);
        SlAccessLogSaveMQ.MsgPayload userDelete = SlAccessLogSaveMQ.parse(msg, SlAccessLogSaveMQ.MsgPayload.class);
        accessLogService.saveAccessLog(userDelete.getAccessLog());
    }
}
